/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 *
 * http://www.ashacker.com
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.validator.Assert;
import io.renren.modules.sys.dao.SysDataDao;
import io.renren.modules.sys.entity.SysDataEntity;
import io.renren.modules.sys.redis.SysDataRedis;
import io.renren.modules.sys.service.SysDataService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("sysDataService")
public class SysDataServiceImpl extends ServiceImpl<SysDataDao, SysDataEntity> implements SysDataService {
	@Autowired
	private SysDataRedis sysDataRedis;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String paramType = (String) params.get("paramType");
		IPage<SysDataEntity> page = this.page(new Query<SysDataEntity>().getPage(params),
				new QueryWrapper<SysDataEntity>().like(StringUtils.isNotBlank(paramType), "param_type", paramType)
						.eq("status", 1));
		return new PageUtils(page);
	}

	@Override
	public void saveData(SysDataEntity data) {
		this.save(data);
		sysDataRedis.saveOrUpdate(data);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysDataEntity data) {
		this.updateById(data);
		sysDataRedis.saveOrUpdate(data);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateValueByKey(String key, String value) {
		baseMapper.updateValueByKey(key, value);
		sysDataRedis.delete(key);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(Long[] ids) {
		for (Long id : ids) {
			SysDataEntity data = this.getById(id);
			sysDataRedis.delete(data.getParamKey());
		}

		this.removeByIds(Arrays.asList(ids));
	}

	@Override
	public String getValue(String key) {
		SysDataEntity data = sysDataRedis.get(key);
		if (data == null) {
			data = baseMapper.queryByKey(key);
			sysDataRedis.saveOrUpdate(data);
		}

		return data == null ? null : data.getParamValue();
	}

	@Override
	public <T> T getDataObject(String key, Class<T> clazz) {
		String value = getValue(key);
		if (StringUtils.isNotBlank(value)) {
			return new Gson().fromJson(value, clazz);
		}

		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw new RRException("获取参数失败");
		}
	}

	@Override
	public List<SysDataEntity> getServiceInfo(String type) {
		List<SysDataEntity> list = baseMapper.selectList(new QueryWrapper<SysDataEntity>().eq("type", type));
		Assert.isNull(list, "客服信息页为空");
		return list;
	}

}
